﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HZ.Supervise.Web.TotalDrugs
{
    public partial class ProcureCatalogStatistics0 : System.Web.UI.Page
    {
        //综合表
        DataTable dt0 = new DataTable();//基本药品统计
        DataTable dt1 = new DataTable();//非基本药品统计
        DataTable dt = new DataTable();//总表统计
        public DataTable dttemp1 = new DataTable();
        public DataTable dttemp2 = new DataTable();
        public DataTable dttemp3 = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            dateBind();
        }

        private void dateBind()
        {
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
             dttemp1 = iPrescription.GetSubsidyStatistics("SELECT SUM(Amount) 采购总金额,SUM(WarehouseAmount) 入库总金额 FROM View_PurchaseOrderDetail ");
             dttemp2 = iPrescription.GetSubsidyStatistics("SELECT SUM(Amount) 采购总金额,SortSource FROM View_PurchaseOrderDetail  where SortSource=0 group by SortSource order by SortSource");
             dttemp3 = iPrescription.GetSubsidyStatistics("select COUNT(1) 可采购品规数,SortSource from TB_ProcureCatalog where IsUsing=1 AND SortSource=0 group by SortSource order by SortSource ");


            dt = iPrescription.GetSubsidyStatistics("select bb.AreaName ,case aa.SortSource when 0 then '基本' else '非基本' end as 采购范围,aa.实际采购品规数,aa.医院数,aa.采购总金额,aa.入库总金额,aa.入库率 from (select  SortSource,COUNT(distinct ProcureCatalogID) 实际采购品规数,COUNT(distinct HospitalID) 医院数,SUM(Amount)采购总金额, SUM(WarehouseAmount) 入库总金额,SUM(WarehouseAmount)/SUM(Amount) 入库率,   left(AreaID,4) aid from View_PurchaseOrderDetail   group by left( AreaID,4),SortSource) aa,TB_Area bb where left(aa.aid,4)=left(bb.AreaID,4) and AreaID %100=0 and aa.SortSource = 0 order by AreaID,采购范围");
            Repeater1.DataSource = dt;
            Repeater1.DataBind();

            dt0 = iPrescription.GetSubsidyStatistics("select bb.AreaName,aa.实际采购品规数,aa.医院数,aa.采购总金额,aa.入库总金额,aa.入库率 from ( select  COUNT(distinct ProcureCatalogID) 实际采购品规数,COUNT(distinct aa.HospitalID) 医院数,SUM(Amount)采购总金额, SUM(WarehouseAmount) 入库总金额,SUM(WarehouseAmount)/SUM(Amount) 入库率, left(aa.AreaID,4) aid from View_PurchaseOrderDetail aa,TB_HospitalInfo bb where aa.HospitalID=bb.HospitalID  and aa.SortSource = 0  group by left( aa.AreaID,4)) aa,TB_Area bb where   left(aa.aid,4)=left(bb.AreaID,4) and AreaID %100=0 order by AreaID");
            Repeater2.DataSource = dt0;
            Repeater2.DataBind();

            dt1.Columns.Add("1", Type.GetType("System.String"));
            dt1.Columns.Add("2", Type.GetType("System.String"));
            dt1.Columns.Add("3", Type.GetType("System.String"));
            dt1.Columns.Add("4", Type.GetType("System.String"));
            dt1.Columns.Add("5", Type.GetType("System.String"));
            dt1.Columns.Add("6", Type.GetType("System.String"));
            dt1.Columns.Add("7", Type.GetType("System.String"));
            dt1.Rows.Add(new object[] { "统计时间", DateTime.Now, null, null, null, null, null });
            dt1.Rows.Add(new object[] { "采购总金额", dttemp1.Rows[0][0], null, null, null, null, null });
            dt1.Rows.Add(new object[] { "入库总金额", dttemp1.Rows[0][1], null, null, null, null, null });
            dt1.Rows.Add(new object[] { "基本药物可采购品规数", dttemp3.Rows[0][0], null, null, null, null, null });
            //dt1.Rows.Add(new object[] { "非基本药物可采购品规数", dttemp3.Rows[1][0], null, null, null, null, null });
            dt1.Rows.Add(new object[] { "基本药物采购总金额", dttemp2.Rows[0][0], null, null, null, null, null });
            // dt1.Rows.Add(new object[] { "非基本药物采购总金额", dttemp2.Rows[1][0], null, null, null, null, null });
            dt1.Rows.Add(new object[] { "基本药物采购总金额/采购总金额", (double.Parse(dttemp2.Rows[0][0].ToString()) / double.Parse(dttemp1.Rows[0][0].ToString()) * 100).ToString("0.00") + "%", null, null, null, null, null });
            // dt1.Rows.Add(new object[] { "非基本药物采购总金额/采购总金额", (double.Parse(dttemp2.Rows[1][0].ToString()) / double.Parse(dttemp1.Rows[0][0].ToString()) * 100).ToString("0.00") + "%", null, null, null, null, null });

            dt1.Rows.Add(new object[] { "采购区域", "采购范围", "实际采购品规数 ", "网上采购医疗机构", "网上订单金额", "实际入库金额", "实际入库金额/订单金额" });
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object[] obj = new object[7];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j == 6)
                    {
                        obj[j] = (double.Parse(dt.Rows[i][j].ToString()) * 100).ToString("0.00") + "%";
                    }
                    else
                    {
                        obj[j] = dt.Rows[i][j];
                    }
                }
                dt1.Rows.Add(obj);
            }
            dt1.Rows.Add(new object[] { " ", " ", " ", " ", " ", " ", " " });
            dt1.Rows.Add(new object[] { " ", " ", " ", " ", " ", " ", " " });
            dt1.Rows.Add(new object[] { "采购区域", "县以下", "实际采购品规数 ", "网上采购医疗机构", "采购总金额", "入库总金额", "入库率" });
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                object[] obj = new object[7];
                for (int j = 0; j < dt0.Columns.Count; j++)
                {
                    if (j == 6)
                    {
                        obj[j] = (double.Parse(dt0.Rows[i][j].ToString()) * 100).ToString("0.00") + "%";
                    }
                    else
                    {
                        obj[j] = dt0.Rows[i][j];
                    }

                }
                dt1.Rows.Add(obj);
            }
            //gvwProduceCatalog.DataSource = dt1;
            //gvwProduceCatalog.DataBind();
        }


        protected void HzButton1_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> newDic = new Dictionary<string, string>();
            newDic.Add("1", "");
            newDic.Add("2", "");
            newDic.Add("3", "");
            newDic.Add("4", "");
            newDic.Add("5", "");
            newDic.Add("6", "");
            newDic.Add("7", "");
            //HZ.Excel.ExcelHelper.ExportDataForWeb(dt1, newDic);
        }
    }
}
